function PriorityQueue() {
    this._array = new Array(32);
    this._size = 0;
}

PriorityQueue.prototype.enQueue = function(key) {
    //队列长度超出范围，扩容
    if(this._size >= this._array.length){
        this.resize();
    }
    this._array[this._size++] = key;
    this.upAdjust();
};

PriorityQueue.prototype.deQueue = function() {
    if(this._size <= 0){
        throw new Error("the queue is empty !");
    }
    //获取堆顶元素
    var head = this._array[0];
    //最后一个元素移动到堆顶
    this._array[0] = this._array[--this._size];
    this.downAdjust();
    return head;
};

PriorityQueue.prototype.upAdjust = function() {
    var childIndex = this._size - 1;
    var parentIndex = Math.floor((childIndex - 1) / 2);
    // temp保存插入的叶子节点值，用于最后的赋值
    var temp = this._array[childIndex];
    while (childIndex > 0 && temp > this._array[parentIndex])
    {
        //无需真正交换，单向赋值即可
        this._array[childIndex] = this._array[parentIndex];
        childIndex = parentIndex;
        parentIndex = Math.floor((parentIndex - 1) / 2);
    }
    this._array[childIndex] = temp;
};

PriorityQueue.prototype.downAdjust = function() {
    // temp保存父节点值，用于最后的赋值
    var parentIndex = 0;
    var temp = this._array[parentIndex];
    var childIndex = 1;
    while (childIndex < this._size) {
        // 如果有右孩子，且右孩子大于左孩子的值，则定位到右孩子
        if (childIndex + 1 < this._size && this._array[childIndex + 1] > this._array[childIndex]) {
            childIndex++;
        }
        // 如果父节点大于任何一个孩子的值，直接跳出
        if (temp >= this._array[childIndex]) {
            break;
        }
        //无需真正交换，单向赋值即可
        this._array[parentIndex] = this._array[childIndex];
        parentIndex = childIndex;
        childIndex = 2 * childIndex + 1;
    }
    this._array[parentIndex] = temp;
};

PriorityQueue.prototype.resize = function() {
    //队列容量翻倍
    var newSize = this._size * 2;
    var newArray = new Array(newSize);
    for(var i = 0; i < this._array.length; i++) {
        newArray[i] = this._array[i];
    }
    this._array = newArray;
};

var priorityQueue = new PriorityQueue();
priorityQueue.enQueue(3);
priorityQueue.enQueue(5);
priorityQueue.enQueue(10);
priorityQueue.enQueue(2);
priorityQueue.enQueue(7);
console.log("出队元素：" + priorityQueue.deQueue());
console.log("出队元素：" + priorityQueue.deQueue());